﻿#light

module PubSim.Glasses

open System
open Microsoft.FSharp.Control

/// Glasses type definition ... used to indicate the number of glasses on the
/// cupboard shelf.
type Glasses(initValue) =
    let mutable numGlasses = initValue
    let lockObj = new Object()

    member g.AddGlass() =
        lock lockObj (fun() -> numGlasses <- numGlasses + 1)
        ignore()
    member g.RemoveGlass() =
        lock lockObj (fun() ->
            if numGlasses > 0 then
                numGlasses <- numGlasses - 1
                true
            else
                false
        )
    member g.AddGlasses(numGlassesToAdd) =
        lock lockObj (fun() -> numGlasses <- numGlasses + numGlassesToAdd)
        ignore()
    member g.RemoveGlasses(numGlassesToRemove) =
        lock lockObj (fun() -> (numGlasses <- numGlasses - numGlassesToRemove))
        ignore()
    member g.GetNumGlasses() = numGlasses
